<template>
	<view class="login-container">
<!-- 提示按钮的图标 -->
<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
<!-- 登录按钮 -->
<button type="primary" class="btn-longin" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
<!-- 登陆提示 -->
<view class="tips-text">登陆后尽享更多权益</view>
	</view>
</template>

<script>
	import {mapMutations} from 'vuex'
	export default {
		methods:{
			...mapMutations('m_user',['updateUserInfo']),
			//用户授权之后， 获取用户的基本信息
			getUserInfo(e){
				// 判断是否获取用户信息成功
				if(e.detail.errMsg==='getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权')
				// console.log(e);
				
				// console.log(e.detail.userInfo)
				this.updateUserInfo(e.detail.userInfo)
				
				// 获取登录成功后的Token字符串
				this.getToken(e.detail)
			},
		async	getToken(info){
				// 获取code对应的值
		const[err,res]=	await	uni.login().catch(err => err)
		// console.log(res)
		if(err||res.errMsg !=='login:ok') return uni.$showMsg('登录失败1111')
	
	// 准备参数
	const query = {
		     code: res.code,
		     encryptedData: info.encryptedData,
		     iv: info.iv,
		     rawData: info.rawData,
		     signature: info.signature
	}
	// 获取token
  const {data:loginResult} = await uni.$http.post('/api/public/v1/users/wxlogin',query)
	console.log('21321321',query);
	if(loginResult.meta.status !== 200) return uni.$showMsg('登录失败')
	
	uni.$showMsg('登录成功')
	
			}
		}
	}
</script>










<style scoped lang="scss">
.login-container{
	height: 100vh;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	background-color: #f8f8f8;
	position: relative;
	overflow: hidden;

	.btn-longin{
		width: 80%;
		border-radius: 50px;
		background-color: #c00000;
		margin: 10px 0;
	}
	.tips-text{
		font-size: 14px;
		color: gray;
	}
}
</style>